/************************
 *  http配置模块          *
 *  作者: WXS              *
 *  date: 2019.6.26     *
 ************************/
import axios from 'axios';
import qs from 'qs';
import globals from './host'
const instance = axios.create({
	baseURL: globals.baseUrl, //调用node服务器地址
	changeOrigin: true, //跨域
	timeout: 60000,
	transformRequest: [function(data) {
		console.log(globals.baseUrl);
		data = JSON.stringify(data);
		return data;
	}]
});


const checkStatus = response => {
	// loading
	// 如果http状态码正常，则直接返回数据
	var data = response.data;
	// if(response.headers.authorization != undefined){
	//     localStorage.setItem('token',response.headers.authorization);
	// }
	return data;
};
// 设置请求等待
instance.interceptors.request.use(
	config => {
		return config;
	},
	error => {}
);
// 设置请求响应后处理
instance.interceptors.response.use(
	response => {
		return checkStatus(response);
	},
	error => {

	}
);

//暴露接口
export default {
	async post(v) {
		let url = v.url
		let data = v.data
		try {
			return await instance({
				method: 'post',
				url,
				dataType: 'json',
				data: data, // 请求时带的参数
				timeout: 30000,
				headers: {
					'Content-Type': 'application/json; charset=UTF-8',
					'Authorization': 'Basic enVpaG91X3VpOnp1aWhvdV91aV9zZWNyZXQ=',
					'tenant': 'eXVleWFuZw==',
					'userid': '',
					'account': '',
					'name': '',
					'token': 'Bearer test',
					'Access-Control-Allow-Origin': 'http://106.52.68.106:10086/'

				},
			});
		} catch (err) {
			console.error(err);
		}
	},
	async get(v) {
		let url = v.url
		let data = v.data
		try {
			return await instance({
				method: 'get',
				url,
				params, // 请求时带的参数
				timeout: 30000,
				headers: {
					'Content-Type': 'application/json; charset=UTF-8',
					'Authorization': 'Basic enVpaG91X3VpOnp1aWhvdV91aV9zZWNyZXQ=',
					'tenant': 'eXVleWFuZw==',
					'userid': '',
					'account': '',
					'name': '',
					'token': 'Bearer test',
					'Access-Control-Allow-Origin': 'http://106.52.68.106:10086/'
				},
			});
		} catch (err) {
			console.error(err);
		}
	},
	instance
};
